<template>
	<view class="page" style="padding-bottom: 150rpx;">
		<image :src="info.image" style="display: block;width: 100%;" mode="widthFix"></image>
		<view class="u-f1 pt2 bg-white p15">
			<view class="fz18 u-font-bold u-line-2">{{info.name}}

				<u-icon @click.stop="toCall(info.mobile)" v-if="info.mobile" style="margin-left: 10rpx;" name="phone"
					class="#666"></u-icon>
			</view>
			<view class="fz15 c6 mt10">
				<u-icon name="clock" class="#666"></u-icon><span class="ml2">{{info.time}}</span>
			</view>
			<view class="fz15 c6 mt10 u-flex" style="position: relative;">
				<u-icon name="map" class="#666"></u-icon><span class="ml2">{{info.address}}</span>

			</view>

			<view class="mt10 u-flex" style="flex-wrap: wrap;" v-if="info.tags && info.tags.length>0">
				<view class="tag u-fex" v-for="(item,index) in info.tags" :key="index">{{item}}</view>
			</view>

		</view>
		<view class="p15">

			<view v-for="(item,index) in productList" :key="index" class="bg-white p12 u-flex brs5 mb10"
				style="align-items: flex-start;">
				<image :src="item.image" style="width:150rpx;height: 150rpx;border-radius: 5rpx;min-width:150rpx">
				</image>
				<view class="pl12 u-f1 pt2">
					<view class="u-flex u-f-jsb">
						<view class="fz16 u-font-bold u-line-2">{{item.name}}</view>

					</view>
					<view class="fz16 u-font-bold mt15 u-flex u-f-jsb" style="color: red;">
						￥{{item.price}}

						<view class="btn" @click="toOrder(item)">去下单</view>
					</view>


				</view>
			</view>
		</view>

		<view class="module-btn u-flex fixed-bottom">
			<button class="u-f-justify u-reset-button btn-nav" @click="toMap">去导航</button>
		</view>

		<u-popup v-model="show" mode="bottom" border-radius="10" :closeable="true">
			<view style="padding: 100rpx 30rpx 100rpx;">

				<view class="bg-white p12 u-flex brs5 mb10" style="align-items: flex-start;">
					<image :src="curProduct.image"
						style="width:120rpx;height: 120rpx;border-radius: 5rpx;min-width:120rpx">
					</image>
					<view class="pl12 u-f1 pt2">
						<view class="u-flex u-f-jsb">
							<view class="fz16 u-font-bold u-line-2">{{curProduct.name}}</view>

						</view>
						<view class="fz16 u-font-bold mt15 u-flex u-f-jsb" style="color: red;">
							￥{{curProduct.price}}

						</view>


					</view>
				</view>
				<view class="">
					<u-input :custom-style="{'font-size':'32rpx'}" @click="toSelect" disabled :clearable="false"
						:height="90" v-model="curCar.code" type="text" :border="true" placeholder="请选择车辆" />
				</view>
				<button style="margin: 50rpx;width: auto;" class="u-f-justify u-reset-button btn-nav"
					@click="toPay">确认支付</button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				page: 1,
				curProduct: {},
				info: {},
				curCar: '',
				isDisabled:false,
				productList: [],
				curId: '',
				loadStatus: '',
			}
		},
		computed: {},
		onLoad(e) {
			this.curId = e.id
			this.initData()

		},
		onShow() {
			if (this.$db.get('carInfo')) {
				this.curCar = this.$db.get('carInfo')
				this.$db.del('carInfo')
			}
		},
		// 页面下拉到底部触发
		onReachBottom() {},
		methods: {

			toCall(phone) {



				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			toOrder(item) {
				if (!this.$db.get("userToken")) {


					this.$common.errorToShow('请先登录！')
					uni.navigateTo({
						url: "/pages/user/auth"
					})
					return
				}
				this.curProduct = item
				this.show = true
			},
			toMap() {
				uni.openLocation({
					latitude: Number(this.info.latitude),
					longitude: Number(this.info.longitude),
					name: this.info.name,
					address: this.info.address,
					success: function() {
						console.log('success');
					}
				});
			},
			wanlPay(code = null) {
				let post = {
					code: code,
					car_id: this.curCar.id,
					shop_id: this.curId,
					good_id: this.curProduct.id,
				}
				this.$api.payment(post, res => {
					if (res.code) {


						uni.requestPayment({
							provider: 'wxpay',
							appId: res.data.appId,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							paySign: res.data.paySign,
							signType: res.data.signType,
							timeStamp: res.data.timeStamp,
							success: (e) => {
								uni.switchTab({
									url: '/pages/order/index'
								})
							},
							fail: (e) => {
								this.isDisabled=false
								// uni.showModal({
								// 	content: "支付失败,原因为: " + e.errMsg,
								// 	showCancel: false
								// })
							}
						})
					} else {
						
						
						this.isDisabled=false
						this.$common.errorToShow(res.msg)
					}
				})

			},
			toPay() {
				if(this.isDisabled){
					return
				}
				this.isDisabled=true
				uni.login({
					success: (e) => {
						this.wanlPay(e.code);
					},
					fail: (e) => {
						
						this.isDisabled=false
						uni.showModal({
							content: "无法获取微信code,原因为: " + e.errMsg,
							showCancel: false
						})
					}
				})
			},
			toSelect() {
				uni.navigateTo({
					url: '/pages/user/car/list?type=select'
				})
			},
			initData() {
				this.$api.shopDetail({
					id: this.curId,
					latitude: 23,
					longitude: 114
				}, res => {
					if (res.code) {
						this.info = res.data
					}
				})
				this.$api.goodsList({
					page: 1,
					pagesize: 50,
				}, res => {
					if (res.code) {
						this.productList = res.data
					}
				})

			},
		}
	}
</script>

<style lang="scss">
	.btn {
		background: #4CA8E7;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #fff;
		padding: 14rpx 32rpx;
	}

	.btn-nav {
		background: #4CA8E7;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #fff;
		width: 100%;
		margin: 0 40rpx;
		height: 90rpx;
	}

	.tag {
		border-radius: 10rpx;
		border: 1px solid #4CA8E7;
		color: #4CA8E7;
		font-size: 24rpx;
		padding: 4rpx 20rpx;
		margin-right: 20rpx;
	}
</style>